home *** CD-ROM | disk | FTP | other *** search
- Path: tudelft.nl!news
- From: Ejo Schrama <schrama@geo.tudelft.nl>
- Newsgroups: comp.lang.c++
- Subject: Can somebody explain me how to do this in C++
- Date: 6 Mar 1996 15:30:32 GMT
- Organization: TU Delft, Faculty of Geodetic Engineering
- Message-ID: <4hkb2o$ko3@mo6.rc.tudelft.nl>
- NNTP-Posting-Host: dutgs7.geo.tudelft.nl
- Mime-Version: 1.0
- Content-Type: text/plain; charset=us-ascii
- Content-Transfer-Encoding: 7bit
- X-Mailer: Mozilla 1.12 (X11; I; HP-UX A.09.05 9000/735)
- X-URL: news:comp.lang.c++
-
- Suppose you've got a base class containing protected data and several
- public methods among which there are some virtual ones. This base class
- is inhereted to "lets call them" subclasses in which the virtual methods
- are implemented. All subclasses are defined during the initialization of a
- problem, however during the execution of a program "where all work is
- executed" you only want to deal with a linked list of base class objects
- which are accessed via a while loop. The code looks as follows:
-
- Cparam *localpointer = root_of_linked_list;
- while (localpointer != NULL) {
- localpointer->execute_some_virtual_method();
- localpointer = localpointer->getlinktonext();
- }
-
- So far there is no problem until the following situation happens. There are
- local variables declared as protected data inside the inhereted subclasses
- (which don't exist in the baseclass) and I want to access those variables
- in the while construction described above.
-
- Any attempt I do to declare
-
- Cparam_inhereted_method *localpointer2 = localpointer;
-
- will always fail since:
-
- class Cparam {
- protected:
- <various data>
- public:
- <various constructors/destructors and methods except execute_routine>
- virtual void execute_some_virtual_method();
- };
-
- class Cparam_inhereted_method : public Cparam {
- protected:
- <various data>
- public:
- <constructors/destructors>
- void execute_some_virtual_method();
- void execute_routine();
- };
-
- so that I can never execute
-
- localpointer2->execute_routine();
-
- The only alternative seems to introduce execute_routine to the Cparam
- base class (which I would like to avoid). How can I avoid this?
-
-